import { useNavTagsView } from "@/pinia/modules/navTagsView";

import { RouteLocationNormalizedLoaded, useRouter } from "vue-router";
import { computed, nextTick, unref } from "vue";

export const useTagsView = () => {
  const tagsViewStore = useNavTagsView();

  const { replace, currentRoute } = useRouter();
  const selectedTag = computed(() => tagsViewStore.selectedTag);
  // const selectedTag = computed(() => tagsViewStore.getSelectedTag)

  const closeAll = (callback?: Fn) => {
    tagsViewStore.deleteAllViews()
    callback?.()
  }

  const closeLeft = (callback?: Fn) => {
    tagsViewStore.deleteLeftViews(
      selectedTag.value as RouteLocationNormalizedLoaded
    );
    callback?.();
  };

  const closeRight = (callback?: Fn) => {
    tagsViewStore.deleteRightViews(
      selectedTag.value as RouteLocationNormalizedLoaded
    );
    callback?.();
  };

  const closeOthers = (callback?: Fn) => {
    tagsViewStore.deleteOthersViews(
      selectedTag.value as RouteLocationNormalizedLoaded
    );
    callback?.();
  };

  const closeCurrent = (
    view?: RouteLocationNormalizedLoaded,
    callback?: Fn
  ) => {    
    if (view?.meta?.affix) return;
    tagsViewStore.deleteView(view || unref(currentRoute));
    callback?.();
  };

  // const refreshPage = async (view?: RouteLocationNormalizedLoaded, callback?: Fn) => {
  //   tagsViewStore.delCachedView()
  //   const { path, query } = view || unref(currentRoute)
  //   await nextTick()
  //   replace({
  //     path: '/redirect' + path,
  //     query: query
  //   })
  //   callback?.()
  // }

  // const setTitle = (title: string, path?: string) => {
  //   tagsViewStore.setTitle(title, path)
  // }

  return {
    closeAll,
    closeLeft,
    closeRight,
    closeOthers,
    closeCurrent,
    // refreshPage,
    // setTitle
  };
};
